package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuangyang.fixboxlib.utils.StringUtils;
import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.NewOrder;


import java.util.ArrayList;

/**
 * Created by vanshu on 15/11/25 10:57.
 */
public class RepairListAdapter extends BaseAdapter {

    private ArrayList<NewOrder> orders = new ArrayList<>();

    public RepairListAdapter(ArrayList<NewOrder> orders) {
        this.orders = orders;
    }

    @Override
    public int getCount() {
        return orders == null ? 0 : orders.size();

    }

    @Override
    public Object getItem(int position) {
        return orders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.repair_list_item_new, parent, false);
            holder = new ViewHolder();
            holder.mOrderTitle = (TextView) convertView.findViewById(R.id.order_title);
            holder.mAppointTime = (TextView) convertView.findViewById(R.id.order_appoint_time);
            holder.mOrderDistance = (TextView) convertView.findViewById(R.id.order_distance);
            holder.mOrderAddress = (TextView) convertView.findViewById(R.id.order_address);
            holder.mPrice = (TextView) convertView.findViewById(R.id.order_start_price);
            holder.mOrderStatus = (ImageView) convertView.findViewById(R.id.order_status);
            holder.mB2BLogo = (ImageView) convertView.findViewById(R.id.b2b_logo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        NewOrder order = orders.get(position);
        holder.mOrderTitle.setText(order.title);
        if((order.dateStatusTip==null)||(order.dateStatusTip.equals(""))){
            holder.mAppointTime.setVisibility(View.INVISIBLE);
        }else {
            holder.mAppointTime.setText(order.dateStatusTip);  //预约时间
            holder.mAppointTime.setVisibility(View.VISIBLE);

        }
        holder.mOrderDistance.setText(StringUtils.convertMeterToKM(Double.parseDouble(order.distance)) + "公里");
        if (order.address2 == null)
            holder.mOrderAddress.setText(order.address1);
        else
            holder.mOrderAddress.setText(order.address1 + order.address2);
        if(order.appType.equals("1000")){
            holder.mB2BLogo.setVisibility(View.VISIBLE);
        }
        if(order.isTimeOut){
            holder.mOrderStatus.setVisibility(View.VISIBLE);
        }else {
            holder.mOrderStatus.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView mAppointTime;
        public ImageView mOrderStatus;
        public TextView mOrderTitle;
        public TextView mOrderAddress;
        public TextView mOrderDistance;
        public TextView mPrice;
        public ImageView mB2BLogo;
    }

}
